import { LoginResult } from "@/api/login";
import { defineStore } from "pinia";
import { computed, ref } from "vue";

// 创建仓库
export const useMemberStore = defineStore(
  "member",
  () => {
    // 个人信息的全局数据
    const profile = ref<LoginResult>({} as LoginResult);

    // 修改个人信息的方法
    const setProfile = (value: LoginResult) => {
      profile.value = value;
    };

    // 判断用户是否登录
    const isLogin = computed(() => {
      return !!profile.value.token;
    });

    // 记得 return
    return { profile, setProfile, isLogin };
  },
  {
    // persist: true,
    persist: {
      // storage: localStorage,
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
        getItem(key) {
          return uni.getStorageSync(key);
        },
      },
    },
  }
);
